package com.atguigu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.MessageConstant;
import com.atguigu.dao.MemberDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl  implements OrderService{
    @Autowired
    OrderSettingDao orderSettingDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderDao orderDao;
    @Override
    public Result addOrder(Map map) throws Exception {
//        1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
            Date orderDate = DateUtils.parseString2Date(map.get("orderDate").toString());
            OrderSetting orderSetting = orderSettingDao.getOrderSetting(orderDate);
            if (orderSetting == null){
                return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }

//        2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
             if (orderSetting.getReservations() >= orderSetting.getNumber()){
                 return new Result(false, MessageConstant.ORDER_FULL);
             }

//        3、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
             String telephone = map.get("telephone").toString();
             Member member = memberDao.getMember(telephone);
             if (member ==null){
                 //自动注册为会员
                 member = new Member();
                 member.setName(map.get("name").toString());
                 member.setPhoneNumber(telephone);
                 member.setSex(map.get("sex").toString());
                 member.setIdCard(map.get("idCard").toString());
                 memberDao.add(member);
             }
//        4、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
              Integer setmealId = Integer.parseInt(map.get("setmealId").toString());
              Integer memberId = member.getId();
              Order order = new Order(); //封装条件
              order.setMemberId(memberId);
              order.setSetmealId(setmealId);
              order.setOrderDate(orderDate);
              Order orderdb = orderDao.getOrder(order);
              if (orderdb != null){
                  return new Result(false,MessageConstant.HAS_ORDERED);
              }
//        5、更新当日的已预约人数
              orderSetting.setReservations(orderSetting.getReservations()+1);
              orderSettingDao.editReservations(orderSetting);
//        6、预约成功
                Order newOrder =  new Order();
                newOrder.setMemberId(memberId);
                newOrder.setSetmealId(setmealId);
                newOrder.setOrderDate(orderDate);
                newOrder.setOrderStatus(Order.ORDERSTATUS_NO);
                newOrder.setOrderType(Order.ORDERTYPE_WEIXIN);
                orderDao.add(newOrder);
                return new Result(true,MessageConstant.ORDER_SUCCESS,newOrder);
    }

    @Override
    public Map  findById(Integer id) throws Exception {
        Map map = orderDao.findById(id);
        Date orderDate = (Date)map.get("orderDate");
        String orderStr = DateUtils.parseDate2String(orderDate);
        map.put("orderDate",orderStr);
        return map;
    }

    @Override
    public List<Map> getSetmealOrderInfo() {
        return orderDao.getSetmealOrderInfo();
    }
}
